       function clearText(thefield) {
            if (thefield.defaultValue == thefield.value)
                thefield.value = "";
       }
              
       function borrar(elemento){
    	   var tr = document.getElementById(elemento);
    	   $(tr).remove();
       }
       
       function showMessage( text ){
    	   $('#tx').text( text );
    	   $('#message').show(300);
       }
       function closeMessage(){
    	   $('#message').hide(300);
       }
       
       function validarTag(){
    	  var val = $('#tag').val();
    	 $.ajax({
    	      url: "/AlianzaMedica/TagServlet",
    	      global: false,
    	      type: "GET",
    	      data: ({tag : val }),
    	      dataType: "xml",
    	      success: function(xml){
    		     process(xml);
    	      }
    	   }
    	);
       }
       
       function process(xml){
    	  var elementos = xml.getElementsByTagName("message");
    	  var length = elementos.length;
    	  var i = 0;
    	  for( i = 0 ; i < length ; i++ ){
    		  var child = elementos.item(i).firstChild.data;
    		  showMessage(child);
    	  }
          
       }
       
       
       
       function createTableRow( item )
       {
    	   var createId = "tid" + item.value;
    	   if( document.getElementById(createId) ){
    		   showMessage("el medicamento ya existe");
    		   return;
    	   }
    	   
    	   var row = document.createElement("tr");
    	   row.setAttribute("id", "tid"+item.value );
    	   var td0 = document.createElement("td");
    	   var text1 = document.createTextNode( item.text );
    	   td0.appendChild(text1);
    	   var td1 = document.createElement("td");
    	   var text2 = document.createTextNode( item.value );
    	   td1.appendChild(text2);
    	   var td2 = document.createElement("td");
    	   var linkBorrado = document.createElement("a");
    	   linkBorrado.setAttribute("href", "javascript:borrar('tid" + item.value + "')");
    	   $(linkBorrado).addClass("removetd");
    	   td2.appendChild(linkBorrado);
    	   var text3 = document.createTextNode("borrar");
    	   linkBorrado.appendChild(text3);
    	   row.appendChild(td1);
    	   row.appendChild(td0);
    	   row.appendChild(td2);
    	   var table = document.getElementById("tbMedicamentos");
    	   table.appendChild( row );
    	   var hiddenId = document.createElement("input");
    	   hiddenId.setAttribute("type","hidden");
    	   hiddenId.setAttribute("name","medicamento");
    	   hiddenId.setAttribute("value",item.value);
    	   row.appendChild(hiddenId);
    	   
       }
       


       $(document).ready(function() {
            var input = $('#drugNameSearch');
            input.click(function(){
            	input.val("");	
            });
            
            var patient = $('#paciente');
            
           /* input.autocomplete('/AlianzaMedica/ajaxServlet',  {
            	extraParams: {"objectType": "Drug" , "like":"true"},
                delay: 10,
                minChars: 2,
                matchSubset: 1,
                selectFirst:false,
                matchContains: true,
                parse: parseXML,
                formatItem: formatItem,
                formatResult: formatResult
            }).result(function(event, item) {
                //window.location.href = item.value
                //$("#drugResult").val(item.value);
                //createTableRow(item);
                $('#drugId').val(item.value);
            });*/
            
            function parseXML(xml) {
                var results = [];
                $(xml).find('Drug').each(function() {
                    var text = $.trim($(this).find('name').text());
                    var value = $.trim($(this).find('id').text());
                    results[results.length] = { 'data': { text: text, value: value },
                        'result': text, 'value': value
                    };
                });
                return results;
            };
            
            function formatItem(data) {
                return data.text;
            };
            
            function formatResult(data) {
                return data.text;
            };
            
            /*patient.autocomplete('/AlianzaMedica/ajaxServlet',  {
            	extraParams: {"objectType": "Patient" , "like":"true"},
                delay: 10,
                minChars: 2,
                matchSubset: 1,
                selectFirst:false,
                matchContains: true,
                parse: parsePatient,
                formatItem: formatItem,
                formatResult: formatResult
            }).result(function(event, item) {
                //window.location.href = item.value
                //$("#drugResult").val(item.value);
                $('#pacienteId').val(item.value);
                //createTableRow(item);
            });*/
            
            function parsePatient(xml) {
                var results = [];
                $(xml).find('Patient').each(function() {
                    var text = $.trim($(this).find('fullName').text());
                    var value = $.trim($(this).find('id').text());
                    results[results.length] = { 'data': { text: text, value: value },
                        'result': text, 'value': value
                    };
                });
                return results;
            };
            
            
            
        });
